#!/usr/bin/php
<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

date_default_timezone_set('UTC'); 
$starttime=time();
echo "Started: ". date('Y-m-d H:i',time())."\n";

define('CURRENT_DIR',dirname(__FILE__));
$last_slash = strrpos(CURRENT_DIR, "/");
define('ROOT_PATH',substr(CURRENT_DIR, 0, $last_slash)."/");

$debugenabled=false;
$maildebugenabled=false;
$dbdebugenabled=false;

error_reporting(0);
ini_set('display_errors', false);

require_once(ROOT_PATH."func/mail.inc.php");
include_once(CURRENT_DIR."/func_stats.php");
include_once(ROOT_PATH."configs/dbconf.php");
require_once(ROOT_PATH."includes/util.func.php");
require_once(ROOT_PATH."class/entity.class.php");
require_once(ROOT_PATH."class/dbmanager.class.php");
require_once(ROOT_PATH."class/loader.class.php");
include_once(ROOT_PATH."func/deltree.inc.php");
require_once(ROOT_PATH."class/files.class.php");
require_once(ROOT_PATH."class/project.class.php");
require_once(ROOT_PATH."class/team.class.php");

require_once(ROOT_PATH."class/def.class.php");

$db = new dbmanager(DB_HOST,DB_USER,DB_PASS);

$db->connect(DB_NAME);
$loader = new loader($db);

/* LOAD DEFINITIONS */
$defs = simplexml_load_file(ROOT_PATH."configs/definitions.xml");
foreach($defs->definition as $def) {
   eval("define('{$def->name}', '{$def->value}');");
}

$defs = simplexml_load_file(ROOT_PATH."configs/tables.xml");
foreach($defs->table as $def) {
   eval("define('{$def->name}', '{$def->value}');");
}

$def = new def;
$list = $loader->loadList($def);

foreach($list as $obj) {
   eval("define('{$obj->id}', '{$obj->value}');");
}

if (ENABLE_CUSTOM_SMTP == 'true') {
	debug("Using custom SMTP");
	ini_set("smtp",SMTP_HOST);
	ini_set("smtp_port",SMTP_PORT);
}

/* END */

$prj = new project;
$prj->status=STATUS_ACTIVE;
$projectlist = $loader->loadListBy($prj,$prj->listByStatus());

$console=true;
$update="true";
foreach ($projectlist as $prj) {
	echo "Processing ".$prj->name."\n";
	$subprjlist = $loader->loadListBy($prj,$prj->listSubProjects());
	foreach ($subprjlist as $subprj) {
		echo ">> Processing ".$subprj->name."\n";
		$team = new team;
		$team->prid=$prj->id;
		$team->subprj=$subprj->id;
		$teamlist = $loader->loadListBy($team,$team->listByReady());	
		foreach ($teamlist as $team) {
			echo ">>>> Processing ".$team->name."\n";
			include(ROOT_PATH."includes/setupfilescreator.php");
		}
	}
}

$endtime=time();
echo "Finished: ". date('Y-m-d H:i',time())."\n";
echo "Process time: ".date("H:i.s",$endtime-$starttime)."\n";


?>